home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE08 / DATADICT / UTILS.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1996-02-07  |  10.5 KB  |  397 lines

  1. unit Utils;
  2. interface
  3. uses sysutils, wintypes,Dialogs, graphics, grids;
  4. const
  5.   IntegerSet : set of ' '..'z' = ['1','2','3','4','5','6','7','8','9','0','-'];
  6.   RealSet : set of ' '..'z' = ['1','2','3','4','5','6','7','8','9','0','-','.','+','E'];
  7.   WordSet : set of ' '..'z' = ['1','2','3','4','5','6','7','8','9','0'];
  8. const
  9.   fmt_10n = '%10.0n';
  10.   fmt_left10n = '%-10.0n';
  11.   pctfmt    = '%3d%%';
  12.  
  13. type                            { from DOS.PAS, RTL 5.5, BP7}
  14.   PathStr = string[79];         { File pathname string }
  15.   DirStr  = string[67];         { Drive and directory string }
  16.   NameStr = string[8];          { File name string }
  17.   ExtStr  = string[4];          { File extension string }
  18. type
  19.   charset = SET OF CHAR;
  20. var
  21.   pathholder : pathstr;
  22.   Dirholder  : dirstr;
  23.   nameholder : namestr;
  24.   extholder  : extstr;
  25.  
  26. {Stringlist & Grid}
  27.  
  28. procedure AdjustColWidth(const whichcol : integer; var whichgrid : TstringGrid);
  29.  
  30. {File name stuff}
  31.  
  32. function AddBackSlash(const S: String): String;
  33. function StripBackSlash(const S: String): String;
  34. function appName : string; {path of application less extension}
  35.  
  36. {String functions}
  37.  
  38. function noSlashstring(const s: string): string;
  39. function StripSymbol(const s, sym: string): string;
  40. FUNCTION find_next_char_position(of_these_char : charset; workline : STRING; start : BYTE) : BYTE;
  41. FUNCTION trimstr(workstr : STRING) : STRING;
  42. FUNCTION leftstr(in_string : STRING; size : BYTE; pad : CHAR) : STRING;
  43.  
  44.  
  45.  
  46. function plural(const s : string): string;
  47.  
  48. function MinimizeName(const Filename: TFileName; Canvas: TCanvas;
  49.   MaxLen: Integer): TFileName;  {from filectrl.pas}
  50.  
  51. {math functions}
  52. function IKMGB(const howbig : longint): string;
  53. function RKMGB(const howbig : real): string;
  54. function maxOf(const first, second : integer):integer;
  55.  
  56. function isIntegerChar(const whatChar : char) : boolean;
  57. function isRealChar(const whatChar : char) : boolean;
  58. function isWordChar(const whatChar : char): boolean;
  59. function IntToCardinalStr(const number : integer): string;
  60.  
  61. {Form appearence -- just a stub for now}
  62. Procedure ScaleForm(sender: Tobject);
  63.  
  64. implementation
  65.  
  66. uses LZExpand, WinProcs, Forms, Controls,
  67.      stdctrls, buttons;
  68.  
  69. {======================== stringlist & gird utilities ==================}
  70.  
  71. procedure AdjustColWidth(const whichcol : integer; var whichgrid : TstringGrid);
  72. var i, x, biggest : integer;
  73. begin with whichgrid do begin
  74.   biggest := 0;
  75.   for i:= 0 to rowcount -1  do
  76.     if canvas.TextWidth(cells[whichCol,i]) > biggest
  77.       then biggest := canvas.textwidth(cells[WhichCol,i]);
  78.   colWidths[longint(whichCol)] := biggest+6;
  79.   end;
  80. end;
  81.  
  82.  
  83.  
  84. function AddBackSlash(const S: String): String;
  85. { Adds a backslash to string S.  If S is already 255 chars or already has }
  86. { trailing backslash, then function returns S. }
  87. begin
  88.   if (Length(S) < 255) and (S[Length(S)] <> '\') then
  89.     Result := S + '\'
  90.   else
  91.     Result := S;
  92. end;
  93.  
  94. function StripBackSlash(const S: String): String;
  95. { Removes trailing backslash from S, if one exists }
  96. begin
  97.   Result := S;
  98.   if Result[Length(Result)] = '\' then
  99.     Dec(Result[0]);
  100. end;
  101.  
  102.  
  103. function appName : string;
  104. begin
  105.   result := copy(application.exename, 1, pos('.',application.exename)-1);
  106. end;
  107.  
  108. function noSlashstring(const s: string): string;
  109. {assumes s is a fully qualified filename}
  110. {takes out '\' and '.'}
  111. {alias name max is dbmaxnamelen,31}
  112. var extra : integer;
  113. begin
  114.   result := s[1]+copy(s,3,255);   {extract the :}
  115.   while pos('\',result) <> 0 do
  116.     result := copy(result, 1, pos('\',result)-1)+
  117.               copy(result, pos('\', result)+1, 255);
  118.   result := copy(result, 1, pos('.', result)-1) +
  119.             copy(result, pos('.', result)+1, 255);
  120.   extra := length(result) - 31;
  121.   if extra > 0
  122.     then result := result[1] + copy(result, extra+1, 255);
  123. end;
  124.  
  125. function StripSymbol(const s, sym: string): string;
  126. {takes out any occurances of symbol}
  127. var extra : integer;
  128. begin
  129.   result := s;
  130.   while pos(sym,result) <> 0 do
  131.     result := copy(result, 1, pos(sym,result)-1)+
  132.               copy(result, pos(sym, result)+1, 255);
  133. end;
  134.  
  135. FUNCTION trimstr(workstr : STRING) : STRING;
  136. VAR first_char, last_char : INTEGER;
  137.   done : BOOLEAN;
  138. BEGIN
  139.   done := FALSE;
  140.   first_char := 1;
  141.   REPEAT
  142.     IF workstr[first_char] <> ' ' THEN done := TRUE
  143.     ELSE INC(first_char);
  144.   UNTIL done OR (first_char = LENGTH(workstr));
  145.   done := FALSE;
  146.   last_char := LENGTH(workstr);
  147.   REPEAT
  148.     IF workstr[last_char] <> ' ' THEN done := TRUE
  149.     ELSE DEC(last_char);
  150.   UNTIL done OR (last_char = 1);
  151.   trimstr := COPY(workstr, first_char, last_char - first_char + 1);
  152. END;
  153.  
  154.  
  155. FUNCTION find_next_char_position(of_these_char : charset; workline : STRING; start : BYTE) : BYTE;
  156. VAR
  157.   i     : INTEGER;
  158.   ch    : CHAR;
  159.   found : boolean;
  160. BEGIN
  161.   found := false;
  162.   FOR i := start TO LENGTH(workline) DO begin
  163.     ch := workline[i];
  164.     IF ch IN of_these_char
  165.       THEN begin found := true; break end;
  166.     end;
  167.   if found
  168.     then result := i
  169.     else result := 0;
  170. END;
  171.  
  172. FUNCTION leftstr(in_string : STRING; size : BYTE; pad : CHAR) : STRING;
  173. VAR i : INTEGER;
  174.     wrkstr : STRING;
  175. BEGIN
  176.   wrkstr := COPY(trimstr(in_string), 1, size);
  177.   IF LENGTH(wrkstr) < size
  178.   THEN
  179.   BEGIN
  180.     FOR i := LENGTH(wrkstr) TO size - 1 DO
  181.       wrkstr := wrkstr + pad
  182.   END;
  183.   leftstr := wrkstr;
  184. END;
  185.  
  186. FUNCTION rightstr(in_string : STRING; size : BYTE; pad : CHAR) : STRING;
  187. VAR i : INTEGER;
  188.     wrkstr : STRING;
  189. BEGIN
  190.   i := LENGTH(trimstr(in_string)) - size + 1;
  191.   IF i <= 0 THEN wrkstr := COPY(trimstr(in_string), 1, size)
  192.   ELSE wrkstr := COPY(trimstr(in_string), i, size);
  193.   IF LENGTH(wrkstr) < size
  194.   THEN
  195.   BEGIN
  196.     FOR i := 1 TO (size - LENGTH(wrkstr)) DO
  197.       wrkstr := pad + wrkstr
  198.   END;
  199.   rightstr := wrkstr;
  200. END;
  201.  
  202.  
  203.  
  204.  
  205. function plural(const s : string): string;
  206. begin end;
  207. {  case s[length(s)] of
  208.     'y' : s := copy(s,}
  209.  
  210. function CurrentTextWidth(cur_canvas : tcanvas; const whatstr : string): integer;
  211.  var TextMetric : tTextMetric;
  212. begin
  213.   getTextMetrics(cur_canvas.handle, textMetric);
  214.   result := (textMetric.tmAveCharWidth * length(whatstr))+2;
  215. end;
  216.  
  217. function CurrentTextHeight(cur_canvas : tcanvas): integer;
  218.  var TextMetric : tTextMetric;
  219. begin
  220.   getTextMetrics(cur_canvas.handle, textMetric);
  221.   result := textMetric.tmHeight + textMetric.tmExternalLeading;
  222. end;
  223.  
  224.  {Generic number formatting}
  225. function IKMGB(const howbig : longint): string;
  226. var i : longint;
  227.     j : real;
  228. const  KBLimit : longint = 1024*1024;
  229.        MBLimit : longint = 1024*1024*1024;
  230.  
  231. begin
  232.   if howbig < 0
  233.     then result := ' '
  234.     else if howbig < 1024
  235.       then result :=  inttostr(howbig)+ ' bytes'
  236.       else if howbig < KBLimit
  237.         then result := format('%n KB',[1.0*howbig/1024])
  238.         else if howbig < MBLimit
  239.           then result := format('%n MB', [1.0*howbig/(1024*1024)])
  240.           else result := format('%n GB', [1.0*howbig/(1024*1024*1024)]);
  241.  end;
  242.  
  243. function RKMGB(const howbig : real): string;
  244. var i : longint;
  245.     j : real;
  246. const  KBLimit : real = 1024*1024;
  247.        MBLimit : real = 1024*1024*1024;
  248.  
  249. begin
  250.   if howbig < 0
  251.     then result := ' '
  252.     else if howbig < 1024
  253.       then result :=  format('%n bytes', [howbig])
  254.       else if howbig < KBLimit
  255.         then result := format('%n KB',[howbig/1024])
  256.         else if howbig < MBLimit
  257.           then result := format('%n MB', [howbig/KBLimit])
  258.           else result := format('%n GB', [howbig/MBLimit]);
  259.  end;
  260.  
  261.  
  262. function maxOf(const first, second : integer):integer;
  263. begin
  264.   if first > second
  265.     then result := first
  266.     else result := second;
  267. end;
  268.  
  269. procedure ChangeToCartesian(var coord : tpoint; const limit : tpoint);
  270. var mid : integer;
  271. begin
  272.   mid := limit.x div 2;
  273.   if coord.x > mid
  274.     then coord.x := coord.x - mid
  275.     else coord.x := - (mid - coord.x);
  276.   mid := limit.y div 2;
  277.   if coord.y > mid
  278.     then coord.y := - (coord.y - mid)
  279.     else coord.y := mid - coord.y;
  280. end;
  281.  
  282. procedure CartesionToPositiveOnly(var coord : tpoint; const limit : tpoint);
  283. var mid : integer;
  284. begin
  285.   mid := limit.x div 2;
  286.   if coord.x > 0
  287.     then coord.x := mid + coord.x
  288.     else if coord.x < 0
  289.       then coord.x := mid + coord.x
  290.       else coord.x := mid;
  291.   mid := limit.y div 2;
  292.   if coord.y > 0
  293.     then coord.y := mid - coord.y
  294.     else if coord.y < 0
  295.       then coord.y := mid + abs(coord.y)
  296.       else coord.y := mid;
  297. end;
  298.  
  299.  
  300.  
  301. function isIntegerChar(const whatChar : char) : boolean;
  302. begin
  303.   if whatChar in IntegerSet
  304.     then result := true
  305.     else result := false;
  306. end;
  307.  
  308. function isRealChar(const whatChar : char) : boolean;
  309. begin
  310.   if whatChar in RealSet
  311.     then result := true
  312.     else result := false;
  313. end;
  314.  
  315. function isWordChar(const whatChar : char): boolean;
  316. begin
  317.   if whatChar in WordSet
  318.     then result := true
  319.     else result := false;
  320. end;
  321.  
  322. function IntToCardinalStr(const number : integer): string;
  323. begin
  324.   result := intToStr(number);
  325.   if copy(result, length(result), 1) = '1'
  326.     then result := result + 'st'
  327.     else if copy(result, length(result),1) = '2'
  328.       then result := result + 'nd'
  329.       else if copy(result, length(result),1) = '3'
  330.         then result := result + 'rd'
  331.         else result := result + 'th';
  332. end;
  333.  
  334.  
  335.  
  336.  procedure CutFirstDirectory(var S: TFileName);
  337. var
  338.   Root: Boolean;
  339.   P: Integer;
  340. begin
  341.   if S = '\' then S := ''
  342.   else begin
  343.     if S[1] = '\' then
  344.     begin
  345.       Root := True;
  346.       S := Copy(S, 2, 255);
  347.     end else Root := False;
  348.     if S[1] = '.' then S := Copy(S, 5, 255);
  349.     P := Pos('\',S);
  350.     if P <> 0 then S := '...\' + Copy(S, P + 1, 255)
  351.     else S := '';
  352.     if Root then S := '\' + S;
  353.   end;
  354. end;
  355.  
  356.  
  357. function MinimizeName(const Filename: TFileName; Canvas: TCanvas;
  358.   MaxLen: Integer): TFileName;
  359. var
  360.   Drive: string[3];
  361.   Dir: TFileName;
  362.   Name: TFileName;
  363.   Ext: TFileName;
  364.   P: Integer;
  365. begin
  366.   Result := FileName;
  367.   Dir := ExtractFilePath(Result);
  368.   Name := ExtractFileName(Result);
  369.   P := Pos('.', Name);
  370.   if P > 0 then Name[0] := Chr(P - 1);
  371.   Ext := ExtractFileExt(Result);
  372.  
  373.   if Dir[2] = ':' then
  374.   begin
  375.     Drive := Copy(Dir, 1, 2);
  376.     Dir := Copy(Dir, 3, 255);
  377.   end else Drive := '';
  378.   while ((Dir <> '') or (Drive <> '')) and (Canvas.TextWidth(Result) > MaxLen) do
  379.   begin
  380.     if Dir = '\...\' then
  381.     begin
  382.       Drive := '';
  383.       Dir := '...\';
  384.     end else if Dir = '' then Drive := ''
  385.     else CutFirstDirectory(Dir);
  386.     Result := Drive + Dir + Name + Ext;
  387.   end;
  388. end;
  389.  
  390. { Scale form ...}
  391. Procedure ScaleForm(sender: Tobject);
  392. begin
  393.   {nothing seems to work very well...}
  394. end;
  395.  
  396. end.
  397.